﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace WPFTemplateLib.WpfConverters
{
    /// <summary>
    /// [转换器] bool 转 Grid 行/列 中的 高/宽（false：高/宽 为 0。true：高/宽 依据参数转换，支持 n*、Auto、具体值。）
    /// </summary>
    public class BoolToGridLengthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            GridLength gridLength = GridLength.Auto;
            try
            {
                if (value is true)
                {
                    string para = parameter + "";
                    if (!string.IsNullOrWhiteSpace(para))
                    {
                        if (para == "Auto" || para == "auto")
                        {
                            //Ignore
                        }
                        else if (para.Contains("*"))
                        {
                            int index = para.IndexOf("*", StringComparison.Ordinal);
                            string numStr = para.Remove(index).Trim();
                            if (string.IsNullOrWhiteSpace(numStr))
                            {
                                numStr = "1";
                            }

                            double.TryParse(numStr, out double num);
                            if (num > 0)
                            {
                                gridLength = new GridLength(num, GridUnitType.Star);
                            }
                            else
                            {
                                gridLength = new GridLength(0);
                            }
                        }
                        else
                        {
                            double.TryParse(para.Trim(), out double num);
                            if (num > 0)
                            {
                                gridLength = new GridLength(num, GridUnitType.Pixel);
                            }
                            else
                            {
                                gridLength = new GridLength(0);
                            }
                        }
                    }
                }
                else
                {
                    gridLength = new GridLength(0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return gridLength;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// [转换器] BoolToGridLengthConverter 的反转版本
    /// </summary>
    public class BoolInvertedToGridLengthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is true)
            {
                return new BoolToGridLengthConverter().Convert(false, targetType, parameter, culture);
            }
            else
            {
                return new BoolToGridLengthConverter().Convert(true, targetType, parameter, culture);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
